package com.LeetCode.StackAndHeap;

import org.junit.Test;

import java.util.Stack;

/**
 * 最小栈
 */
public class MinStack {

    Stack<Integer> dataStack;

    Stack<Integer> minStack;

    public MinStack() {
        dataStack = new Stack<>();
        minStack = new Stack<>();
    }

    public void push(int x){
        dataStack.push(x);
        if(minStack.isEmpty() || x <= minStack.peek()){
            minStack.push(x);
        }
    }

    public void pop() {
        int x = dataStack.pop();
        if(x == minStack.peek())
            minStack.pop();
    }

    public int top(){
        return dataStack.peek();
    }

    public int getMin(){
        return minStack.peek();
    }

    @Test
    public void test(){
        MinStack minStack = new MinStack();
        minStack.push(0);
        minStack.push(1);
        minStack.push(0);
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.getMin());

    }
}
